package com.springboot.springboot_demo.utils;

import java.io.*;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;

/**
 * Title: FileDigest.class <br>
 * Description:MD5 比较两个照片是否相同 <br>
 * Create DateTime:2022/6/14 11:05 <br>
 *
 * @author peichang.yang
 */
public class ImgDigestUtil {

    /**
     * 
     * 获取单个文件的MD5值！
     * 
     * @param file
     * 
     * @return
     * 
     */

    public static String getFileMD5(File file) {

        if (!file.isFile()) {
            return null;
        }

        MessageDigest digest = null;

        FileInputStream in = null;

        byte buffer[] = new byte[1024];

        int len;
        try {

            digest = MessageDigest.getInstance("MD5");

            in = new FileInputStream(file);

            while ((len = in.read(buffer, 0, 1024)) != -1) {

                digest.update(buffer, 0, len);

            }
            in.close();

        } catch (Exception e) {

            e.printStackTrace();

            return null;
        }
        BigInteger bigInt = new BigInteger(1, digest.digest());

        return bigInt.toString(16);

    }

    /**

     * 获取文件夹中文件的MD5值

     * @param file

     * @param listChild ;true递归子目录中的文件

     * @return

     */

    public static Map getDirMD5(File file, boolean listChild) {

        if(!file.isDirectory()){
            return null;
        }
        Map map=new HashMap();

        String md5;

        File files[]=file.listFiles();

        for(int i = 0; i <files.length;i++){
        File f=files[i];
        if(f.isDirectory()&&listChild){
            map.putAll(getDirMD5(f, listChild));
        } else {
            md5=getFileMD5(f);

            if(md5!=null){

                map.put(f.getPath(), md5);
            }
        }
    }
    return map;

}

    public static void main(String[] args) {

        File file1 = new File(
                "https://newimg.4001113900.com/img/201804/order/order152375356593941.jpg");

        String s = file1.getPath();

        File file2 = new File(
                "https://newimg.4001113900.com/img/201804/order/order152375356593941.jpg");

        System.out.println(getFileMD5(file1).equals(getFileMD5(file2)));

        System.out.println(s);

    }













   /********************************************_________***************************************/

    /**
     * 根据图片url 读取字节数
     *
     * @param name
     * @return
     */
    public static byte[] getByte(String name) {
        // 得到文件长度
        File file = new File(name);
        byte[] b = new byte[(int) file.length()];
        System.out.println(file.length());
        try {
            InputStream in = new FileInputStream(file);
            try {
                in.read(b);
                System.out.println(in.read());
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        return b;
    }


    /**
     * 加密
     *
     * @param s
     * @return
     */
    public static String MD5(byte[] s) {
        // 16进制字符
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        try {
            byte[] strTemp = s;
            MessageDigest mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(strTemp);
            byte[] md = mdTemp.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            // 移位 输出字符串
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            return null;
        }
    }
    
   /* public static void main(String[] args) {
        String img1 = MD5(getByte("d:/da7bc06b1c1c612c0ba0780974bede9.jpg"));
        String img2 = MD5(getByte("d:/da7bc06b1c1c612c0ba080974bede9 - 副本.jpg"));
        if (img1.equals(img2))
            System.out.println("两图片是一样的");
        else
            System.out.println("两图片是不一样的");
    }*/
}
